home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / AMarquee / examples / streamgen.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  2KB  |  91 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. #include <dos/dos.h>
  7. #include <clib/dos_protos.h>
  8. #include <clib/exec_protos.h>
  9.  
  10. #include <clib/AMarquee_protos.h>
  11.  
  12. #include <pragmas/AMarquee_pragmas.h>
  13.  
  14. #define UNLESS(x) if (!(x))
  15.  
  16. struct Library * AMarqueeBase = NULL;
  17. struct QSession * session     = NULL;
  18.  
  19. void CleanExit(void)
  20. {
  21.   if (session)      QFreeSession(session);       /* This MUST be done before we close the library! */
  22.   if (AMarqueeBase) CloseLibrary(AMarqueeBase);
  23.   printf("All done.\n");
  24. }
  25.  
  26. /* Main program--simply increments our counter using a stream. */
  27. int main(int argc, char ** argv)
  28. {
  29.   char * connectTo, * progName;
  30.   int port = 2957;
  31.   int count = 0;
  32.   
  33.   atexit(CleanExit);
  34.     
  35.   printf("StreamGen: This program continually updates an integer on the server.\n");
  36.   printf("           It uses QStreamOp(), so anyone subscribing to streamcount\n");
  37.   printf("           should see a monotonously increasing stream of integers.\n\n");
  38.   printf("Usage Note :  StreamGen [host=hostname] [name=streamgen]\n");
  39.  
  40.   connectTo = (argc > 1) ? argv[1] : "localhost";
  41.   progName  = (argc > 2) ? argv[2] : "streamgen";
  42.  
  43.   if ((AMarqueeBase = OpenLibrary("amarquee.library",37L)) == NULL)
  44.   {
  45.     printf("Couldn't open amarquee.library v37!\n");
  46.     exit(RETURN_ERROR);
  47.   }
  48.   printf("Connecting to %s:%i...\n",connectTo, port);
  49.   if ((session = QNewSession(connectTo, port, progName)) == NULL)
  50.   {
  51.     printf("Couldn't connect to server %s:%i\n",connectTo, port);
  52.     CloseLibrary(AMarqueeBase);
  53.     exit(RETURN_WARN);
  54.   }
  55.   printf("StreamGen connected to server %s:%i\n",connectTo, port);
  56.  
  57.   /* Setup */
  58.   while(1)
  59.   {
  60.     struct QMessage * msg;
  61.     
  62.     printf("Streaming %i...\r",count); fflush(stdout);
  63.     UNLESS(QStreamOp(session, "streamcount", &count, sizeof(count)))
  64.     {
  65.       printf("QStreamOp() (even) failed, aborting!\n");
  66.       break;    
  67.     } 
  68.     count++;
  69.     UNLESS(QGo(session, 0L))
  70.     {
  71.       printf("QGo() failed, aborting!\n");
  72.       break;
  73.     }    
  74.     Delay(10);
  75.     
  76.     /* Check for any error messages */
  77.     while(msg = (struct QMessage *)GetMsg(session->qMsgPort))
  78.     {
  79.       int stat = msg->qm_Status;
  80.       
  81.       FreeQMessage(session, msg);
  82.       if (stat != QERROR_NO_ERROR)
  83.       {
  84.         printf("Received an error message, closing the connection\n");
  85.         exit(RETURN_ERROR);
  86.       }
  87.     }
  88.   }
  89.   /* CleanExit called here via the atexit() feature */
  90. }
  91.